﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ProjectEulerSolutions
{
    /*
     * The points P (x1, y1) and Q (x2, y2) are plotted at integer co-ordinates and are joined to the origin, O(0,0), to form ΔOPQ.

There are exactly fourteen triangles containing a right angle that can be formed when each co-ordinate lies between 0 and 2 inclusive; that is,
0 ≤ x1, y1, x2, y2 ≤ 2.

Given that 0 ≤ x1, y1, x2, y2 ≤ 50, how many right triangles can be formed?

     * */
    class Problem91 : IProblem
    {
        struct PointTuple
        {
            public int x1;
            public int x2;
            public int y1;
            public int y2;

            public PointTuple(int x1, int y1, int x2, int y2)
            {
                if (x1 * x1 + y1 * y1 > x2 * x2 + y2 * y2)
                {
                    this.x1 = x1;
                    this.y1 = y1;
                    this.x2 = x2;
                    this.y2 = y2;
                }
                else
                {
                    this.x1 = x2;
                    this.y1 = y2;
                    this.x2 = x1;
                    this.y2 = y1;
                }
            }
        }

        public string Calculate()
        {
            long count = 0;
            int n = 50;

            for (int j = 1; j <= n; j++)
            {
                for (int i = 1; i <= n; i++)
                {
                    long gcd = CommonFunctions.GreatestCommonDivisor(i, j);

                    int reciprocX = -(int)(j / gcd);
                    int reciprocY = (int)(i / gcd);
                    int x = i + reciprocX;
                    int y = j + reciprocY;

                    while (x >= 0 && y >= 0 && x <= n && y <= n)
                    {
                        count+=2;
                        x += reciprocX;
                        y += reciprocY;
                    }
                }
            }
            count += 3 * n * n;

            return count.ToString();
        }

    }
}
